


//
//  WXShareTool.m
//  QingQing
//
//  Created by yang on 2018/12/18.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "WXShareTool.h"

@implementation WXShareTool

+ (void)wxShareWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url defaultImage:(UIImage *)defaultImage type:(ShareStyle)type successBlock:(void(^)(void))successBlock failBlock:(void(^)(void))failBlock{
        SendMessageToWXReq *req1 = [[SendMessageToWXReq alloc]init];
        req1.bText =  NO;
        if (type == ShareStyleWechat) {
            req1.scene = WXSceneSession;
        }else if (type == ShareStyleFriendCircle){
            req1.scene = WXSceneTimeline;
        }else{
            NSLog(@"不知分享聊天或朋友圈");
            return;
        }
        //创建分享内容对象
        WXMediaMessage *urlMessage = [WXMediaMessage message];
        urlMessage.title = title;//分享标题
        urlMessage.description = content;//分享描述
        [urlMessage setThumbImage:defaultImage];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
        //创建多媒体对象
        WXWebpageObject *webObj = [WXWebpageObject object];
        webObj.webpageUrl = url;//分享链接
        //完成发送对象实例
        urlMessage.mediaObject = webObj;
        req1.message = urlMessage;
        //发送分享信息
    [WXApi sendReq:req1 completion:^(BOOL success) {
        if (success) {
            successBlock();
        }else{
            failBlock();
        }
    }];
}

+(void) wxShareWithImage:(NSString *)imgStr type:(ShareStyle)type successBlock:(void(^)(void))successBlock failBlock:(void(^)(void))failBlock{
    ///获取图片
    NSData *tmpData = [NSData dataWithContentsOfURL:URLWithString(imgStr)];
    UIImage *img = [UIImage imageWithData:tmpData];
    NSData *imageData = UIImageJPEGRepresentation(img, 0.7);
       
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = imageData;

    WXMediaMessage *message = [WXMediaMessage message];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"res5"
                                                         ofType:@"jpg"];
    message.thumbData = [NSData dataWithContentsOfFile:filePath];
    message.mediaObject = imageObject;

    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    if (type == ShareStyleWechat) {
        req.scene = WXSceneSession;
    }else if (type == ShareStyleFriendCircle){
        req.scene = WXSceneTimeline;
    }else{
        NSLog(@"不知分享聊天或朋友圈");
        return;
    }
    [WXApi sendReq:req completion:^(BOOL success) {
        if (success) {
            successBlock();
        }else{
            failBlock();
        }
    }];
}

+ (void)wxMiniProgramShareWithUserName:(NSString *)userName path:(NSString *)path webpageUrl:(NSString *)webpageUrl hdImage:(UIImage *)hdImage withShareTicket:(BOOL)withShareTicket miniprogramType:(WXMiniProgramType)miniprogramType title:(NSString *)title{
    WXMiniProgramObject *object = [WXMiniProgramObject object];
    object.webpageUrl = webpageUrl;
    object.userName = userName;
    object.path = path;
    NSData *thumbData = UIImageJPEGRepresentation(hdImage, 0.7);
    object.hdImageData = thumbData;
    object.withShareTicket = withShareTicket;
    object.miniProgramType = miniprogramType;
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = @"小程序描述";
    message.thumbData = nil;  //兼容旧版本节点的图片，小于32KB，新版本优先
    //使用WXMiniProgramObject的hdImageData属性
    message.mediaObject = object;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;  //目前只支持会话
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
}


@end
